/**
 * @开发版权 
 * @项目名称 VFCloud-Rendering
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2023-02-21
 * @修订日期
 * @描述  ApplicationReadyEventListener
 */
package com.vf.cloud.rendering.common.listener;

import java.io.File;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.vf.cloud.rendering.common.constant.Cache;
import com.vf.cloud.rendering.common.constant.SoftPath;
import com.vf.cloud.rendering.common.util.FileUtil;
import com.vf.cloud.rendering.common.vo.*;
import com.vf.cloud.rendering.server.dispatch.DispatchServer;
import com.vf.cloud.rendering.server.relay.RelayServer;
import com.vf.cloud.rendering.server.signalling.SignallingServer;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

	@Override
	public void onApplicationEvent(ApplicationReadyEvent contextReadyEvent) {

//		try {
//
//			String macAddress = NetUtil.getMacAddress(InetAddress.getLocalHost());
//
//			RenderingConfig renderingConfig = RenderingConfig.dao
//					.findFirst("select * from " + RenderingConfig.TABLE_NAME);
//			if (renderingConfig == null) {
//				renderingConfig = new RenderingConfig();
//				renderingConfig.setId(macAddress);
//				renderingConfig.setUserName("admin");
//				renderingConfig.setPassword("R@123456");
//				renderingConfig.save();
//
//				log.warn("请先配置渲染参数.");
//				return;
//			}
//
//			renderingConfig.setId(macAddress);
//			renderingConfig.update();
//
//			if (StrKit.isBlank(renderingConfig.getSignallingIp())
//					|| StrKit.isBlank(renderingConfig.getSignallingPort())) {
//				log.warn("请先配置信令服务参数.");
//				return;
//			}
//
//			// 启动信令服务
//			SignallingServer.getInstance().restart();
//
//			if (!StrKit.isBlank(renderingConfig.getTurnIp()) && !StrKit.isBlank(renderingConfig.getTurnPort())
//					&& !StrKit.isBlank(renderingConfig.getStunPort())) {
//			} else {
//				log.warn("中继服务参数缺失，未能启动.");
//			}
//
//			if (!StrKit.isBlank(renderingConfig.getDispatchIp()) && !StrKit.isBlank(renderingConfig.getDispatchPort())
//					&& !StrKit.isBlank(renderingConfig.getDispatchSecretKey())) {
//			} else {
//				log.warn("调度服务参数缺失，未能启动.");
//			}
//
//		} catch (UnknownHostException e) {
//			e.printStackTrace();
//			log.error("请配置网络.", e);
//		}

		String configPath = String.format("%s/RenderingServer/config/server.ini", SoftPath.path);
		File f = new File(configPath);
		if (!f.exists()) {
			log.warn("渲染服务未设置配置信息.");
			Cache.isRunning = true;
			return;
		}

		String text = FileUtil.readConfig();
		if (StrKit.isBlank(text)) {
			log.warn("渲染服务未设置配置信息.");
			Cache.isRunning = true;
			return;
		}

		try {
			JSONObject object = JSONObject.parseObject(text);
			if (object == null) {
				log.warn("渲染服务未设置配置信息.");
				System.exit(0);
				return;
			}

			if (!object.containsKey("local")) {
				log.warn("渲染服务未设置配置信息.");
				System.exit(0);
				return;
			}

			if (object.containsKey("local")) {
				Cache.local = JSONObject.parseObject(object.getString("local"), Local.class);
			}

			if (Cache.local == null || StrKit.isBlank(Cache.local.getIp()) || StrKit.isBlank(Cache.local.getMac())
					|| StrKit.isBlank(Cache.local.getProjectPath())) {
				log.warn("请配置local信息.");
				System.exit(0);
				return;
			}

			if (object.containsKey("signalling")) {
				Cache.signalling = JSONObject.parseObject(object.getString("signalling"), Signalling.class);
				SignallingServer.getInstance().stop();
			}

			if (object.containsKey("dispatch")) {
				Cache.dispatch = JSONObject.parseObject(object.getString("dispatch"), Dispatch.class);
				DispatchServer.getInstance().stop();
			} else {
				log.warn("未配置调度接入点...");
			}

			if (object.containsKey("relay")) {
				Cache.relay = JSONObject.parseObject(object.getString("relay"), Relay.class);
				RelayServer.getInstance().restart();
			}

			if (object.containsKey("projectPath")) {
				Cache.projectPath = object.getString("projectPath");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			Cache.isRunning = true;
		}
	}

}
